import com.sun.media.sound.RIFFReader;

import java.util.List;

public class MySingleList implements IList {

    //把ListNode 节点 定义为内部类
    static class ListNode {
        public int val;//存储数据

        public ListNode next;//存储下一个节点的地址

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//代表当前链表的头节点引用(随时可能变化)

    public void createLink() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(45);
        ListNode node3 = new ListNode(23);
        ListNode node4 = new ListNode(90);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        this.head = node1;
    }


    //头插法
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        //链表为空时
        if (head == null) {
            this.head = node;
            return;
        }
        node.next = this.head;
        this.head = node;
    }


    //尾插法
    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        //链表为空时
        if (this.head == null) {
            this.head = node;
            return;
        }
        ListNode cur = this.head;
        //寻找最后一个节点
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    private void checkIndex(int index) throws ListIndexOutOfException {
        if (index < 0 || index > size()) {
            throw new ListIndexOutOfException("任意位置插入下标不合法："
                    + index);
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    @Override
    public void addIndex(int index, int data) {
        checkIndex(index);
        //头插法
        if (index == 0) {
            addFirst(data);
            return;
        }
        //尾插法
        if (index == size()) {
            addLast(data);
            return;
        }

        //往中间位置任意插入
        ListNode cur = searchPrev(index);
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

    //找到 index - 1 位置下标
    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        int count = 0;
        //让cur 走index - 1 步
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }


    //查找是否包含关键字key是否在单链表当中
    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        //遍历比较是否包含该key
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }


    //删除第一次出现关键字为key的节点
    @Override
    public void remove(int key) {
        if (head == null) {
            return;//链表为空
        }

        //判断删除第一个节点
        if (head.val == key) {
            head = head.next;
            return;
        }

        ListNode cur = findPrev(key);
        if (cur == null) {
            System.out.println("没有你要删除的节点key！");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    /**
     * 找到 关键字 key的前一个节点(前驱)
     *
     * @return
     */
    private ListNode findPrev(int key) {
        ListNode cur = this.head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }


    //删除所有值为key的节点(遍历一遍)
    @Override
    public void removeAllKey(int key) {
        //判断链表是否为空
        if (head == null) {
            return;
        }
        /*while (head.val == key) {
            head = head.next;
        }*/
        //准备一个快慢节点，用来记录前驱节点
        ListNode prev = head;
        ListNode cur = head.next;

        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
        //最后判断第一个节点的val值是否为key
        if (head.val == key) {
            head = head.next;
        }
    }


    @Override
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    @Override
    public void clear() {
        //暴力置空
        //this.head = null;
        //
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.val = 0;
            cur.next = null;
            cur = curNext;
        }
        //手动置空head
        head = null;
    }

    //遍历打印链表
    @Override
    public void display() {
        //用一个变量代替head，防止找不到头(第一个节点)
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    /**
     * 这个是从指定位置开始打印
     *
     * @param newHead
     */
    public void display(ListNode newHead) {
        //用一个变量代替newHead，防止找不到头(第一个节点)
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    //反转一个单链表
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        //只有一个节点
        if (head.next == null) {
            return head;
        }

        ListNode cur = head.next;

        head.next = null;
        while (cur != null) {
            //curNext 记录cur中的next
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }


    //给定一个带有头结点 head 的非空单链表，返回链表的中间结点。
    //如果有两个中间结点，则返回第二个中间结点。
    public ListNode middleNode1(ListNode head) {
        //1、求数组长度
        ListNode cur = this.head;
        int len = size();
        //2、求长度/2 找到这个中间节点
        //让cur走 长度/2 步
        for (int i = 0; i < len / 2; i++) {
            cur = cur.next;
        }
        return cur;
    }

    //利用快慢指针
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }


    //输出倒数第k个节点
    public ListNode FindKthToTail(int k) {
        if (k <= 0 || head == null) {
            return null;
        }

        ListNode fast = this.head;
        ListNode slow = this.head;

        for (int i = 0; i < k - 1; i++) {
            fast = fast.next;
            //处理 K 太大的问题
            if (fast == null) {
                return null;
            }
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }

        return slow;
    }


    //链表的回文结构
    public boolean chkPalindrome(ListNode head) {
        if (head == null) {
            return true;
        }
        if (head.next == null) {
            return true;
        }

        ListNode fast = head;
        ListNode slow = head;

        //1.找到中间节点 利用快慢指针
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }

        //2. 反转slow后面的链表 利用头插法
        ListNode cur = slow.next;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }

        //3.从后往前 从前往后
        while (head != slow) {
            if (head.val != slow.val) {
                return false;
            }
            //偶数节点情况的判断
            if (head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    public ListNode partition(ListNode head, int x) {
        if (head == null) {
            return null;
        }
        ListNode cur = head;
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        while (cur != null) {
            if (cur.val < x) {
                // 小于 x 第一次插入，用尾插法
                if (bs == null) {
                    bs = cur;
                    be = cur;
                } else {

                    be.next = cur;
                    be = be.next;
                }
            } else {
                // 大于 x 第一次插入，用尾插法
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }

        //可能存在全部小于x 或全部大于等于x的数据
        if (bs == null) {//包含第一段为空和链表为空的情况
            return as;
        }
        //第一段不为空
        be.next = as;//走到这里be(第一段)一定不为空

        if (ae != null) {//这里需要判读一下第二段是否为空
            ae.next = null;
        }
        return bs;
    }

    //给定一个链表，判断链表中是否有环
    public boolean hasCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            //有环一定会相遇
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return false;
        }
        return true;
    }


    //给定一个链表，返回链表开始入环的第一个节点。
    // 如果链表无环，则返回 NULL。
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            //有环一定会相遇
            if (fast == slow) {
                break;
            }
        }
        if (fast == null || fast.next == null) {
            return null;
        }
        //代码走到这一定有环
        slow = head;
        while (slow != fast) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }
}
